package com.sky.wsp.es.service.impl;

import com.sky.wsp.es.dao.ESBookRepository;
import com.sky.wsp.es.vo.Book;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author geng
 * 2020/12/19
 */
@Service
public class BookService {
    private Logger log = LoggerFactory.getLogger(BookService.class);
    @Resource
    private ESBookRepository esBookRepository;

    public void addBook(Book book) {
        try {
            esBookRepository.save(book);
        } catch (Exception e) {
            log.error(String.format("保存ES错误！%s", e.getMessage()));
        }
    }

    public List<Book> searchBook(String keyword) {
        return esBookRepository.findByTitleOrAuthor(keyword, keyword);
    }

    public SearchHits<Book> searchBook1(String keyword) {
        return esBookRepository.find(keyword);
    }
}
